//
//  CZCommentCell.swift
//  GZWeibo007
//
//  Created by apple on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

import SDWebImage

class CZCommentCell: UITableViewCell {

    ///评论模型
    var comment: CZComment? {
        didSet {
            nameLabel.text = comment?.user?.screen_name
            timeLabel.text = comment?.created_at
            contentLabel.text = comment?.text
            iconView.sd_setImageWithURL(NSURL(string: (comment?.user?.profile_image_url)!), placeholderImage: nil)
        }
    }
    
    func rowHeight(comment: CZComment) -> CGFloat {
        self.comment = comment
        layoutIfNeeded()
        return CGRectGetMaxY(contentLabel.frame) + 2
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareForUI()
    }

    private func prepareForUI() {
        contentView.addSubview(iconView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(zanBtn)
        
        //添加约束
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: 30, height: 30), offset: CGPoint(x: 8, y: 8))
        
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: timeLabel, size: nil, offset: CGPoint(x: 0, y: 5))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 300))
        
        zanBtn.ff_AlignInner(type: ff_AlignType.TopRight, referView: contentView, size: CGSize(width: 18, height: 18), offset: CGPoint(x: -12, y: 12))
    }
    
    
    
    
    
    
    
    
    
    //MARK:- 懒加载子控件
    
    ///评论人头像
    private lazy var iconView: UIImageView = {
        let image = UIImage(named: "avatar_default_big")
        let imageView = UIImageView(image: image)
        imageView.layer.cornerRadius = 15
        imageView.clipsToBounds = true
        return imageView
    }()
    
    ///评论人名字
    private lazy var nameLabel: UILabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 11)
  
    ///评论时间
    private lazy var timeLabel: UILabel = UILabel(color: UIColor.orangeColor(), fontSize: 9)
    
    ///评论内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.blackColor(), fontSize: 12)
        label.preferredMaxLayoutWidth = 300
        label.numberOfLines = 0
        return label
    }()
    
    ///点赞按钮
    private lazy var zanBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "timeline_icon_unlike"), forState: UIControlState.Normal)
        return btn
    }()
}
